<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="部门名称" prop="depName">
      <el-input v-model="dataForm.depName" placeholder="部门名称"></el-input>
    </el-form-item>
    <el-form-item label="部门描述" prop="depInfo">
      <el-input v-model="dataForm.depInfo" placeholder="部门描述"></el-input>
    </el-form-item>
    <el-form-item label="部门人数" prop="depNumber">
      <el-input v-model="dataForm.depNumber" placeholder="部门人数"></el-input>
    </el-form-item>
    <el-form-item label="是否启用" prop="depState">
      <el-input v-model="dataForm.depState" placeholder="是否启用"></el-input>
    </el-form-item>
    <el-form-item label="部门编码" prop="depCode">
      <el-input v-model="dataForm.depCode" placeholder="部门编码"></el-input>
    </el-form-item>
    <el-form-item label="部门地址" prop="depAddr">
      <el-input v-model="dataForm.depAddr" placeholder="部门地址"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          depId: 0,
          depName: '',
          depInfo: '',
          depNumber: '',
          depState: '',
          depCode: '',
          depAddr: ''
        },
        dataRule: {
          depName: [
            { required: true, message: '部门名称不能为空', trigger: 'blur' }
          ],
          depInfo: [
            { required: true, message: '部门描述不能为空', trigger: 'blur' }
          ],
          depNumber: [
            { required: true, message: '部门人数不能为空', trigger: 'blur' }
          ],
          depState: [
            { required: true, message: '是否启用不能为空', trigger: 'blur' }
          ],
          depCode: [
            { required: true, message: '部门编码不能为空', trigger: 'blur' }
          ],
          depAddr: [
            { required: true, message: '部门地址不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.depId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.depId) {
            this.$http({
              url: this.$http.adornUrl(`/code/depinfo/info/${this.dataForm.depId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.depName = data.depInfo.depName
                this.dataForm.depInfo = data.depInfo.depInfo
                this.dataForm.depNumber = data.depInfo.depNumber
                this.dataForm.depState = data.depInfo.depState
                this.dataForm.depCode = data.depInfo.depCode
                this.dataForm.depAddr = data.depInfo.depAddr
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/code/depinfo/${!this.dataForm.depId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'depId': this.dataForm.depId || undefined,
                'depName': this.dataForm.depName,
                'depInfo': this.dataForm.depInfo,
                'depNumber': this.dataForm.depNumber,
                'depState': this.dataForm.depState,
                'depCode': this.dataForm.depCode,
                'depAddr': this.dataForm.depAddr
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
